import $ from 'jquery';
import QRCode from 'qrcodejs2';
import { Base64 } from 'js-base64';
import { nameConfig } from '@theme/vm';
import '../style/download-web.scss';
const SITE_NAME = process.env.SITE_NAME || '';
const imgPath = SITE_NAME + '_appdown';
import { getEnTitleName } from '@/common/js/common';

// 判断是否是手机浏览器
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);

function redirectToDownloadIfMobile() {
  // 获取当前路径
  const currentPath = window.location.pathname;

  // 检查路径是否包含 download_com.html
  const pathContainsDownloadCom = currentPath.includes('download_com.html');

  // 如果是手机浏览器并且路径包含 download_com.html，跳转到 download.html
  if (isMobile && pathContainsDownloadCom) {
    window.location.href =
      window.location.origin + '/download.html' + window.location.search;
  }
}

function GetUrlParam(): any {
  const url = decodeURI(window.location.href);
  if (isMobile) {
    redirectToDownloadIfMobile();
    return null;
  }
  const thisParam = new Object();
  // 判断是否存在请求的参数
  if (url.indexOf('?') !== -1) {
    const str = url.split('?')[1];
    // 截取所有请求的参数，以数组方式保存
    const strs = str.split('&');
    for (let i = 0; i < strs.length; i++) {
      // 获取该参数名称，值。其中值以unescape()方法解码，有些参数会加密
      thisParam[strs[i].split('=')[0]] = strs[i].split('=')[1];
    }
  }
  // 返回改参数列表对象
  return thisParam;
}

function setSourceUrl() {
  let code = '';
  const i_code = GetUrlParam()?.i_code;
  const r_code = GetUrlParam()?.r_code;
  if (i_code) {
    code = '?i_code=' + i_code;
  } else if (r_code) {
    code = '?r_code=' + r_code;
  }
  return code;
}

function createQRCode(text?: string) {
  try {
    const qrcode = new QRCode(document.getElementById('offical_qrcode'), {
      text,
      width: 152,
      height: 152,
      colorDark: '#000000',
      colorLight: '#ffffff',
      correctLevel: QRCode.CorrectLevel.H,
    });

    $('#offical_qrcode').show();
  } catch (e) {
    const qrcode = new QRCode(document.getElementById('offical_qrcode'), {
      text: window.location.href,
      width: 152,
      height: 152,
      colorDark: '#000000',
      colorLight: '#ffffff',
      correctLevel: QRCode.CorrectLevel.H,
    });

    $('#offical_qrcode').show();
  }
}

/**
 * web入口文件
 * @export
 * @param {*} type 区分全站体育
 */
export default function (type: 'sport' | 'site') {
  if (['69', 'wg'].indexOf(nameConfig?.brand) >= 0) {
    document.title =
      Base64.decode(nameConfig.welComeText) +
      Base64.decode(nameConfig.sportName);
  } else {
    document.title =
      getEnTitleName(nameConfig?.brand) + Base64.decode(nameConfig.sportName);
  }

  const originUrl = window.location.origin + setSourceUrl();
  console.log(originUrl);
  createQRCode(originUrl);
  // 设置体育和全站图片
  if (type === 'sport') {
    $('#banner').attr(
      'src',
      `/resource_images/${imgPath}/sport/appdown_sports.png?__static_hash__`
    );
  } else {
    $('#banner').attr(
      'src',
      `/resource_images/${imgPath}/app/appdown_content_01.png?__static_hash__`
    );
  }
}
